package com.brzjomo.secretiveworld.inventory.container;

import java.util.Objects;

import com.brzjomo.secretiveworld.init.ModBlocks;
import com.brzjomo.secretiveworld.init.ModContainerTypes;
import com.brzjomo.secretiveworld.inventory.slot.NoChestSlot;
import com.brzjomo.secretiveworld.tileentity.CorpseOfAnUnknownTravelerTileEntity;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IWorldPosCallable;

public class CorpseOfAnUnknownTravelerContainer extends Container {

	public final CorpseOfAnUnknownTravelerTileEntity tileEntity;
	private final IWorldPosCallable canInteractWithCallable;
	private int customRow = 1;
	private int customColumn = 3;
	private int inventorySize = customRow * customColumn;
	public static int inventorySize2 = 1 * 3;

	public CorpseOfAnUnknownTravelerContainer(final int windowId, final PlayerInventory playerInventory,
			final CorpseOfAnUnknownTravelerTileEntity tileEntity) {
		super(ModContainerTypes.CORPSE_OF_AN_UNKNOWN_TRAVELER.get(), windowId);
		this.tileEntity = tileEntity;
		this.canInteractWithCallable = IWorldPosCallable.of(tileEntity.getWorld(), tileEntity.getPos());

		// 容器部分
		int startX = 62;
		int startY = 26;
		int slotSize = 18;
		for (int row = 0; row < customRow; ++row) {
			for (int column = 0; column < customColumn; ++column) {
				this.addSlot(new NoChestSlot(tileEntity, (row * customColumn) + column, startX + (column * slotSize),
						startY + (row * slotSize)));
			}
		}

		// 玩家部分
		int originX = 8;
		int startPlayerInvY = 66;
		for (int row = 0; row < 3; ++row) {
			for (int column = 0; column < 9; ++column) {
				this.addSlot(new Slot(playerInventory, 9 + (row * 9) + column, originX + (column * slotSize),
						startPlayerInvY + (row * slotSize)));
			}
		}

		// 快捷栏部分
		int hotbarY = 124;
		for (int column = 0; column < 9; ++column) {
			this.addSlot(new Slot(playerInventory, column, originX + (column * slotSize), hotbarY));
		}
	}

	private static CorpseOfAnUnknownTravelerTileEntity getTileEntity(final PlayerInventory playerInventory,
			final PacketBuffer data) {
		Objects.requireNonNull(playerInventory, "playerInventory cannot be null");
		Objects.requireNonNull(data, "data cannot be null");
		final TileEntity tileAtPos = playerInventory.player.world.getTileEntity(data.readBlockPos());
		if (tileAtPos instanceof CorpseOfAnUnknownTravelerTileEntity) {
			return (CorpseOfAnUnknownTravelerTileEntity) tileAtPos;
		}
		throw new IllegalStateException("Tile entity is not correct! " + tileAtPos);
	}

	public CorpseOfAnUnknownTravelerContainer(final int windowId, final PlayerInventory playerInventory,
			final PacketBuffer data) {
		this(windowId, playerInventory, getTileEntity(playerInventory, data));
	}

	@Override
	public boolean canInteractWith(PlayerEntity playerIn) {
		return isWithinUsableDistance(canInteractWithCallable, playerIn, ModBlocks.CORPSE_OF_AN_UNKNOWN_TRAVELER.get());
	}

	@Override
	public ItemStack transferStackInSlot(PlayerEntity playerIn, int index) {
		ItemStack itemstack = ItemStack.EMPTY;
		Slot slot = this.inventorySlots.get(index);
		if (slot != null && slot.getHasStack()) {
			ItemStack itemstack1 = slot.getStack();
			itemstack = itemstack1.copy();
			if (index < inventorySize) {
				if (!this.mergeItemStack(itemstack1, inventorySize, this.inventorySlots.size(), true)) {
					return ItemStack.EMPTY;
				}
			} else if (!this.mergeItemStack(itemstack1, 0, inventorySize, false)) {
				return ItemStack.EMPTY;
			}

			if (itemstack1.isEmpty()) {
				slot.putStack(ItemStack.EMPTY);
			} else {
				slot.onSlotChanged();
			}
		}
		return itemstack;
	}
}
